package com.shujia.kafka

import java.util.Properties

import org.apache.kafka.clients.producer.{KafkaProducer, ProducerRecord}

object Demo1produce {
  def main(args: Array[String]): Unit = {

    /**
      * 1、创佳生产者
      *
      */

    val properties = new Properties

    //1、指定kafkabroker地址
    properties.setProperty("bootstrap.servers", "master:9092,node1:9092,node2:9092")

    //2、指定kv的序列化类
    properties.setProperty("key.serializer", "org.apache.kafka.common.serialization.StringSerializer")
    properties.setProperty("value.serializer", "org.apache.kafka.common.serialization.StringSerializer")


    val producer = new KafkaProducer[String, String](properties)


    //构建一行数据，指定topic 和数据
    //如果topic 不存在会自动创建一个分区为1  副本为1的topic
    val record = new ProducerRecord[String, String]("words", "java")

    //2、将数据写入kafka
    producer.send(record)


    //将数据刷到kafkaz中
    producer.flush()


    //关闭kafka链接
    producer.close()


  }
}
